package service

import ("git.oschina.net/fanbuchi/xgggh/oauth2/model"
	client "git.oschina.net/fanbuchi/xgggh/oauth2/proto/client"
	"git.oschina.net/fanbuchi/xgggh/oauth2/db"
)

func  ClientSearchByClientId(clientId string) (*model.Client,error) {
	req:=client.ClientSearchReq{Params:map[string]string{"key":clientId}}
	result,err:=ClientSearch(&req)
	if err !=nil{
		return nil,err
	}
	if len(result)<=0 {
		return nil,nil
	}
	return result[0],err
}

func  ClientSearch(req *client.ClientSearchReq) ([]*model.Client,error) {
	var result [] *model.Client;
	//page
	limit,offset:=SearchPage(req.Page,req.PerPage)

	//support below data filter
	params:=model.Client{
		Key: req.Params["key"],
		Secret:req.Params["secret"],
		RedirectUri:req.Params["redirectUri"],
	}
	//db
	err:=db.GetDB().
		Where(params).
		Offset(offset).
		Limit(limit).
		Find(&result).
		Error
	return  result,err
}
